package org.jeecg.modules.demo.base.service.impl;

import org.jeecg.modules.demo.base.entity.ZnzfScoreStandard;
import org.jeecg.modules.demo.base.entity.ZnzfScoreStandardItem;
import org.jeecg.modules.demo.base.mapper.ZnzfScoreStandardItemMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfScoreStandardMapper;
import org.jeecg.modules.demo.base.service.IZnzfScoreStandardService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 计分标准
 * @Author: jeecg-boot
 * @Date:   2022-03-27
 * @Version: V1.0
 */
@Service
public class ZnzfScoreStandardServiceImpl extends ServiceImpl<ZnzfScoreStandardMapper, ZnzfScoreStandard> implements IZnzfScoreStandardService {

	@Autowired
	private ZnzfScoreStandardMapper znzfScoreStandardMapper;
	@Autowired
	private ZnzfScoreStandardItemMapper znzfScoreStandardItemMapper;
	
	@Override
	@Transactional
	public void saveMain(ZnzfScoreStandard znzfScoreStandard, List<ZnzfScoreStandardItem> znzfScoreStandardItemList) {
		znzfScoreStandardMapper.insert(znzfScoreStandard);
		if(znzfScoreStandardItemList!=null && znzfScoreStandardItemList.size()>0) {
			for(ZnzfScoreStandardItem entity:znzfScoreStandardItemList) {
				//外键设置
				entity.setScoreStandardId(znzfScoreStandard.getId());
				znzfScoreStandardItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ZnzfScoreStandard znzfScoreStandard,List<ZnzfScoreStandardItem> znzfScoreStandardItemList) {
		znzfScoreStandardMapper.updateById(znzfScoreStandard);
		
		//1.先删除子表数据
		znzfScoreStandardItemMapper.deleteByMainId(znzfScoreStandard.getId());
		
		//2.子表数据重新插入
		if(znzfScoreStandardItemList!=null && znzfScoreStandardItemList.size()>0) {
			for(ZnzfScoreStandardItem entity:znzfScoreStandardItemList) {
				//外键设置
				entity.setScoreStandardId(znzfScoreStandard.getId());
				znzfScoreStandardItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		znzfScoreStandardItemMapper.deleteByMainId(id);
		znzfScoreStandardMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			znzfScoreStandardItemMapper.deleteByMainId(id.toString());
			znzfScoreStandardMapper.deleteById(id);
		}
	}
	
}
